<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8" />
  <title>单例模式</title>
</head>
<body>
    <button onclick="liucheng()">进店开吃</button>
</body>
</html>
<script>
  //餐厅类
  function Diningroom(money,seat,worker){
      this.money = money
      this.seat = seat
      this.worker = worker
      this.danli = null
      this.menu = []
      this.addMenu = function (obj){
        for(let i = 0;i<obj.length;i++){
          this.menu.push(obj[i])
        }
      }
      this.recruit= function(obj){
        console.log('招聘方法')
        this.worker.push(obj)
      }
      this.fire = function(obj){
        console.log('解雇方法')
        for(let i=0;i<this.worker.length;i++){
          if(obj.id == this.worker[i].id){
            this.worker.splice(i,1)
            return
          }
        }
      }

  }
  Diningroom.createDanli = function(money,seat,worker){
      if(!this.danli){
        return this.danli = new Diningroom(money,seat,worker)
      }
      return this.danli
  }

  // 职员类
  function Worker(id,name,pay){
       this.id = id
       this.name = name
       this.pay = pay 
       this.isArry = function(obj){
          return Object.prototype.toString.call(obj)== '[object Array]';
       }
  }
  // 服务员类
  function Waiter(id,name,pay){
    Worker.call(this,id,name,pay)
    this.type = 1
    this.instance = null
    this.tongzhiCooker = function(obj){
       console.log('通知厨师做'+ obj[0].name)
       return obj
    }
  }
  Waiter.prototype = Object.create(Worker.prototype);
  Waiter.careteNew = function(id,name,pay){
      if(!this.instance){
        return this.instance = new Waiter(id,name,pay)
      }
      return this.instance
  }
  
  // 厨师类
  function Cooker(id,name,pay){
    Worker.call(this,id,name,pay)
    this.type = 2
    this.instance = null

  }
  Cooker.prototype = Object.create(Worker.prototype);
  Cooker.careteNew = function(id,name,pay){
    if(!this.instance){
        return this.instance = new Cooker(id,name,pay)
      }
      return this.instance
  }
  // 顾客类
  function Visitor(){
     this.order = function(){
       let index  = Math.round(Math.random());
       let obj = canding.menu[index];
       console.log('顾客点菜'+ obj.name)
       return [obj]
     }
     this.eat = function(obj){
       console.log('顾客吃' + obj.name)
     }
  }
  // 菜品类
  function Food(name,cost,price){
       this.name = name
       this.cost = cost
       this.price = price
  }

  Worker.prototype.complete = function(obj){
      // console.log(typeof obj )
      if(this.type == 1){
        if(this.isArry(obj)){
          // console.log(obj)
          console.log('服务员记录客人点菜' + obj[0].name)
          return obj
        }
        console.log('服务员上菜' + obj.name)
        return obj
       
       
      }else if(this.type == 2){
        // console.log(obj)
        console.log('厨师做菜' + obj[0].name)
        return obj[0]
      }
        
  }

 let server =  Waiter.careteNew(1,'lison','1000')

 let cooker = Cooker.careteNew(3,'do',3000)

 let canding= Diningroom.createDanli(1000,1,[server,cooker])

 let redMeat = new Food('红烧肉',20,40)

 let fish = new Food('鱼香肉丝',10,20)

 canding.addMenu([redMeat,fish])

 let yyh = new Visitor()

//  console.log(cooker,cooker,canding)

function liucheng (){

 let cai  = yyh.order()//顾客点菜

 let jilu = server.complete(cai)//服务员记录

 let jieshou = server.tongzhiCooker(jilu)//通知厨师

 let food = cooker.complete(jieshou) //厨师做菜

 let serFood = server.complete(food) //服务员上菜

 yyh.eat(serFood)//顾客吃菜

}
 








</script>
